package leetcode.view1705;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public String[] findLongestSubarray(String[] array) {
		Map<Integer, Integer> map = new HashMap<>();
		int len = 0, index = -2;
		map.put(0, -1);
		for (int i = 0, c = 0, d = 0; i < array.length; i++) {
			char ch = array[i].charAt(0);
			if ((ch >= 'a' && ch <= 'z') || (ch>='A' && ch<='Z')) {//Character.isLetter
				c++;
			} else {
				d++;
			}
			int diff = c - d;
			if (map.containsKey(diff)) {
				int cur = i - map.get(diff);
				if (cur > len) {
					len = cur;
					index = map.get(diff);
				}
			} else {
				map.put(diff, i);
			}
		}
		if (index == -2) {
			return new String[] {};
		}
		String[] ans = new String[len];
		for (int i = index + 1, j = 0; j < len; i++, j++) {
			ans[j] = array[i];
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.print(this.findLongestSubarray(H.str2array2("[\"A\",\"1\"]")));
		H.print(this.findLongestSubarray(H.str2array2("[\"A\",\"1\",\"B\",\"C\",\"D\",\"2\",\"3\",\"4\",\"E\",\"5\",\"F\",\"G\",\"6\",\"7\",\"H\",\"I\",\"J\",\"K\",\"L\",\"M\"]")));
		H.print(this.findLongestSubarray(H.str2array2("[\"A\",\"A\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}

